<?php defined('SYSPATH') or die('No direct script access.');

abstract class Controller_Common extends Controller_Template {

    public $postfix;
    public $_config;
    public $lang;
    public $sitetext;

	public function before() {
        parent::before();
        $this->_config = Kohana::$config->load('main');

        if (!$this->request->param('lang') || $this->request->param('lang') == 'ru') {
            $lang = 'ru';
            $this->lang = 'ru';
        } else {
            $lang = $this->request->param('lang');
            $this->postfix = '_'.$lang;
            $this->lang = $lang;
        }

        $this->sitetext = ORM::factory('Sitetext')->getAll($this->postfix);
        View::set_global('title', $this->sitetext['title']['_value']);
        View::set_global('description', $this->sitetext['meta_description']['_value']);
        View::set_global('lang', $lang);
        View::set_global('postfix', $this->postfix);
        View::set_global('sitetext', $this->sitetext);
	}

    public function buildCarcas() {
        $this->template->header = View::factory('pieces/header', array(
            'menu' => ORM::factory('Menu')->getAll($this->postfix),
            'menu_count' => ORM::factory('Menu')->count_all(),
            'c_url' => $c_uri = Request::detect_uri()
        ));
        $this->template->footer = View::factory('pieces/footer');
    }

    public function getOffset($per_page) {
        $currentPage = $this->request->param('page');

        if ($currentPage == 1 || empty($currentPage)) {
            return 0;
        } else {
            return $currentPage * $per_page - $per_page;
        }
    }

}
